package com.example.BMS_Disp_1;

import java.text.NumberFormat;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class CellAdapter extends BaseAdapter {
    private Context mContext;
    // references to our images
    private final String KEY_COUNT="count";
    
    private int mCount; 
    private boolean mTextView;
    private BatteryDB mDB;
    private NumberFormat mVoltageFormat = NumberFormat.getInstance();
    
//    private TextView mBattery;
    private ImageView[] mBattery = new ImageView[mCount];
    private TextView[] mBatteryVal = new TextView[mCount];
    
    public CellAdapter(Context c, BatteryDB db, boolean txtView) {
    	mContext = c;
    	mDB = db;
    	
    	mCount = mDB.getConfigValue(KEY_COUNT);
        mVoltageFormat.setMaximumFractionDigits(2);
        mVoltageFormat.setMinimumFractionDigits(2);

    	mTextView = txtView;
    	if (!mTextView) {
    		mBattery = new ImageView[mCount];
    		for (int i=0;i<mCount;i++) {
    			mBattery[i] = new ImageView (mContext);
    			mBattery[i].setLayoutParams(new GridView.LayoutParams(175,175));
 //   			mBattery[i].setPadding(0, 8, 0, 8);
    			mBattery[i].setImageResource(R.drawable.battery_bg);
    			mBattery[i].setScaleType(ImageView.ScaleType.CENTER_CROP);
    		}
    	} else {
    		mBatteryVal = new TextView[mCount];
    		for (int i=0;i<mCount;i++) {
    			mBatteryVal[i] = new TextView (mContext);
    			mBatteryVal[i].setLayoutParams(new GridView.LayoutParams(175,175));
 //   			mBatteryVal[i].setPadding(8, 8, 8, 8);
    			mBatteryVal[i].setTextAppearance(mContext, R.style.CodeFont);
            	mBatteryVal[i].setBackgroundResource(R.color.white);
            	mBatteryVal[i].setGravity(android.view.Gravity.CENTER);
    		}
    	}
    }

 	/*
    private TextView[] mBatteries = new TextView[mCount];
    public CellAdapter(Context c) {
        mContext = c;
//        ClipDrawable cd = new ClipDrawable(new ShapeDrawable(new RectShape()),android.view.Gravity.BOTTOM, 2);
//       cd.setLevel(5000);
//        cd.setColorFilter(R.color.white,android.graphics.PorterDuff.Mode.DST_OVER);
//        cd.setVisible(true,true);
        for (int i=0;i<mCount;i++) {
        	mBatteries[i] = new TextView(mContext);
        	mBatteries[i].setLayoutParams(new GridView.LayoutParams(85, 85));
        	mBatteries[i].setPadding(8, 8, 8, 8);
        	mBatteries[i].setEnabled(true);
        	mBatteries[i].setBackgroundResource(R.color.green);
//        	mBatteries[i].setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.battery));
        	mBatteries[i].setBackgroundDrawable(cd);
        	mBatteries[i].setText("3.12");
//        	mBatteries[i].setTextColor(R.color.black);
        	mBatteries[i].setTextAppearance(mContext, R.style.CodeFont);
        	mBatteries[i].setGravity(android.view.Gravity.CENTER);
    	}
    }
*/
    
    public int getCount() {
        return mCount;
    }

    public Object getItem(int position) {
    	Log.w("getItem",Integer.toString(position));
    	return mTextView? mBatteryVal[position]: mBattery[position];
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
    	double voltage = mDB.getVoltage(position+1);	// position is a 0-based index. Whereas cellIds are 1-based.
    	// BUGBUG -- error check?
    	if (voltage <= 0) {
    		Log.e("getView", "No voltage for cell=" + (position+1));
    		voltage=0;
    	}
    	if (mTextView) {
    		mBatteryVal[position].setText(Integer.toString(position+1) + "\n" + mVoltageFormat.format(voltage));
    		return mBatteryVal[position];
    	} else {
        	int index = (int)(voltage * 10);
    		mBattery[position].setImageLevel(index);
    		return mBattery[position];
    	}
    }	

}